/*
 * Copyright (c) 2023 vitasystems GmbH
 *
 * This file is part of project EHRbase Migration Tool
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ehrbase.migration.exporter.v0.jooq.pg.tables;

import com.nedap.archie.rm.datastructures.ItemStructure;
import java.sql.Timestamp;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import org.ehrbase.migration.exporter.v0.jooq.binding.OtherDetailsJsonbBinder;
import org.ehrbase.migration.exporter.v0.jooq.binding.SysPeriodBinder;
import org.ehrbase.migration.exporter.v0.jooq.pg.Ehr;
import org.ehrbase.migration.exporter.v0.jooq.pg.Indexes;
import org.ehrbase.migration.exporter.v0.jooq.pg.Keys;
import org.ehrbase.migration.exporter.v0.jooq.pg.tables.records.StatusHistoryRecord;
import org.ehrbase.migration.exporter.v0.jooq.pg.udt.records.DvCodedTextRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Function14;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Records;
import org.jooq.Row14;
import org.jooq.Schema;
import org.jooq.SelectField;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

/**
 * This class is generated by jOOQ.
 */
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class StatusHistory extends TableImpl<StatusHistoryRecord> {

    private static final long serialVersionUID = 1L;

    /**
     * The reference instance of <code>ehr.status_history</code>
     */
    public static final StatusHistory STATUS_HISTORY = new StatusHistory();

    /**
     * The class holding records for this type
     */
    @Override
    public Class<StatusHistoryRecord> getRecordType() {
        return StatusHistoryRecord.class;
    }

    /**
     * The column <code>ehr.status_history.id</code>.
     */
    public final TableField<StatusHistoryRecord, UUID> ID =
            createField(DSL.name("id"), SQLDataType.UUID.nullable(false), this, "");

    /**
     * The column <code>ehr.status_history.ehr_id</code>.
     */
    public final TableField<StatusHistoryRecord, UUID> EHR_ID =
            createField(DSL.name("ehr_id"), SQLDataType.UUID, this, "");

    /**
     * The column <code>ehr.status_history.is_queryable</code>.
     */
    public final TableField<StatusHistoryRecord, Boolean> IS_QUERYABLE =
            createField(DSL.name("is_queryable"), SQLDataType.BOOLEAN, this, "");

    /**
     * The column <code>ehr.status_history.is_modifiable</code>.
     */
    public final TableField<StatusHistoryRecord, Boolean> IS_MODIFIABLE =
            createField(DSL.name("is_modifiable"), SQLDataType.BOOLEAN, this, "");

    /**
     * The column <code>ehr.status_history.party</code>.
     */
    public final TableField<StatusHistoryRecord, UUID> PARTY =
            createField(DSL.name("party"), SQLDataType.UUID.nullable(false), this, "");

    /**
     * The column <code>ehr.status_history.other_details</code>.
     */
    public final TableField<StatusHistoryRecord, ItemStructure> OTHER_DETAILS =
            createField(DSL.name("other_details"), SQLDataType.JSONB, this, "", new OtherDetailsJsonbBinder());

    /**
     * The column <code>ehr.status_history.sys_transaction</code>.
     */
    public final TableField<StatusHistoryRecord, Timestamp> SYS_TRANSACTION =
            createField(DSL.name("sys_transaction"), SQLDataType.TIMESTAMP(6).nullable(false), this, "");

    /**
     * The column <code>ehr.status_history.sys_period</code>.
     */
    public final TableField<StatusHistoryRecord, SimpleEntry<java.time.OffsetDateTime, java.time.OffsetDateTime>>
            SYS_PERIOD = createField(
                    DSL.name("sys_period"),
                    org.jooq.impl.DefaultDataType.getDefaultDataType("\"pg_catalog\".\"tstzrange\"")
                            .nullable(false),
                    this,
                    "",
                    new SysPeriodBinder());

    /**
     * The column <code>ehr.status_history.has_audit</code>.
     */
    public final TableField<StatusHistoryRecord, UUID> HAS_AUDIT =
            createField(DSL.name("has_audit"), SQLDataType.UUID.nullable(false), this, "");

    /**
     * The column <code>ehr.status_history.attestation_ref</code>.
     */
    public final TableField<StatusHistoryRecord, UUID> ATTESTATION_REF =
            createField(DSL.name("attestation_ref"), SQLDataType.UUID, this, "");

    /**
     * The column <code>ehr.status_history.in_contribution</code>.
     */
    public final TableField<StatusHistoryRecord, UUID> IN_CONTRIBUTION =
            createField(DSL.name("in_contribution"), SQLDataType.UUID.nullable(false), this, "");

    /**
     * The column <code>ehr.status_history.archetype_node_id</code>.
     */
    public final TableField<StatusHistoryRecord, String> ARCHETYPE_NODE_ID = createField(
            DSL.name("archetype_node_id"),
            SQLDataType.CLOB
                    .nullable(false)
                    .defaultValue(DSL.field(DSL.raw("'openEHR-EHR-EHR_STATUS.generic.v1'::text"), SQLDataType.CLOB)),
            this,
            "");

    /**
     * The column <code>ehr.status_history.name</code>.
     */
    public final TableField<StatusHistoryRecord, DvCodedTextRecord> NAME = createField(
            DSL.name("name"),
            org.ehrbase.migration.exporter.v0.jooq.pg.udt.DvCodedText.DV_CODED_TEXT.getDataType(),
            this,
            "");

    /**
     * The column <code>ehr.status_history.sys_tenant</code>.
     */
    public final TableField<StatusHistoryRecord, Short> SYS_TENANT = createField(
            DSL.name("sys_tenant"),
            SQLDataType.SMALLINT.defaultValue(DSL.field(DSL.raw("1"), SQLDataType.SMALLINT)),
            this,
            "");

    private StatusHistory(Name alias, Table<StatusHistoryRecord> aliased) {
        this(alias, aliased, null);
    }

    private StatusHistory(Name alias, Table<StatusHistoryRecord> aliased, Field<?>[] parameters) {
        super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
    }

    /**
     * Create an aliased <code>ehr.status_history</code> table reference
     */
    public StatusHistory(String alias) {
        this(DSL.name(alias), STATUS_HISTORY);
    }

    /**
     * Create an aliased <code>ehr.status_history</code> table reference
     */
    public StatusHistory(Name alias) {
        this(alias, STATUS_HISTORY);
    }

    /**
     * Create a <code>ehr.status_history</code> table reference
     */
    public StatusHistory() {
        this(DSL.name("status_history"), null);
    }

    public <O extends Record> StatusHistory(Table<O> child, ForeignKey<O, StatusHistoryRecord> key) {
        super(child, key, STATUS_HISTORY);
    }

    @Override
    public Schema getSchema() {
        return aliased() ? null : Ehr.EHR;
    }

    @Override
    public List<Index> getIndexes() {
        return Arrays.asList(Indexes.EHR_STATUS_HISTORY, Indexes.STATUS_HISTORY_EHR_IDX);
    }

    @Override
    public List<ForeignKey<StatusHistoryRecord, ?>> getReferences() {
        return Arrays.asList(Keys.STATUS_HISTORY__STATUS_HISTORY_SYS_TENANT_FKEY);
    }

    private transient Tenant _tenant;

    /**
     * Get the implicit join path to the <code>ehr.tenant</code> table.
     */
    public Tenant tenant() {
        if (_tenant == null) _tenant = new Tenant(this, Keys.STATUS_HISTORY__STATUS_HISTORY_SYS_TENANT_FKEY);

        return _tenant;
    }

    @Override
    public StatusHistory as(String alias) {
        return new StatusHistory(DSL.name(alias), this);
    }

    @Override
    public StatusHistory as(Name alias) {
        return new StatusHistory(alias, this);
    }

    @Override
    public StatusHistory as(Table<?> alias) {
        return new StatusHistory(alias.getQualifiedName(), this);
    }

    /**
     * Rename this table
     */
    @Override
    public StatusHistory rename(String name) {
        return new StatusHistory(DSL.name(name), null);
    }

    /**
     * Rename this table
     */
    @Override
    public StatusHistory rename(Name name) {
        return new StatusHistory(name, null);
    }

    /**
     * Rename this table
     */
    @Override
    public StatusHistory rename(Table<?> name) {
        return new StatusHistory(name.getQualifiedName(), null);
    }

    // -------------------------------------------------------------------------
    // Row14 type methods
    // -------------------------------------------------------------------------

    @Override
    public Row14<
                    UUID,
                    UUID,
                    Boolean,
                    Boolean,
                    UUID,
                    ItemStructure,
                    Timestamp,
                    SimpleEntry<java.time.OffsetDateTime, java.time.OffsetDateTime>,
                    UUID,
                    UUID,
                    UUID,
                    String,
                    DvCodedTextRecord,
                    Short>
            fieldsRow() {
        return (Row14) super.fieldsRow();
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Function)}.
     */
    public <U> SelectField<U> mapping(
            Function14<
                            ? super UUID,
                            ? super UUID,
                            ? super Boolean,
                            ? super Boolean,
                            ? super UUID,
                            ? super ItemStructure,
                            ? super Timestamp,
                            ? super SimpleEntry<java.time.OffsetDateTime, java.time.OffsetDateTime>,
                            ? super UUID,
                            ? super UUID,
                            ? super UUID,
                            ? super String,
                            ? super DvCodedTextRecord,
                            ? super Short,
                            ? extends U>
                    from) {
        return convertFrom(Records.mapping(from));
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Class,
     * Function)}.
     */
    public <U> SelectField<U> mapping(
            Class<U> toType,
            Function14<
                            ? super UUID,
                            ? super UUID,
                            ? super Boolean,
                            ? super Boolean,
                            ? super UUID,
                            ? super ItemStructure,
                            ? super Timestamp,
                            ? super SimpleEntry<java.time.OffsetDateTime, java.time.OffsetDateTime>,
                            ? super UUID,
                            ? super UUID,
                            ? super UUID,
                            ? super String,
                            ? super DvCodedTextRecord,
                            ? super Short,
                            ? extends U>
                    from) {
        return convertFrom(toType, Records.mapping(from));
    }
}
